package com.taotao.sso.controller;

import com.baobao.common.pojo.TaotaoResult;
import com.baobao.common.utils.CookieUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.taotao.sso.pojo.User;
import com.taotao.sso.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.reflection.ExceptionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import sun.org.mozilla.javascript.internal.Token;
import sun.security.jgss.LoginConfigImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Qin PengCheng
 * @date 2018/6/26
 */
@Controller
@RequestMapping("user")
public class UserController {


    @Autowired
    private UserService userService;

    @RequestMapping(value="{viewName}",method=RequestMethod.GET)
    public String goPage(@PathVariable("viewName")String viewName){

        return viewName;
    }

    //验证参数   True：数据可用，false：数据不可用
    @RequestMapping(value="/check/{param}/{type}",method=RequestMethod.GET)
    public ResponseEntity<Boolean> checkUser(@PathVariable("param")String param,@PathVariable("type")Integer type){

        try {
            Boolean flag=this.userService.checkUser(param,type);
            return ResponseEntity.ok(flag);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    //注册功能
    @RequestMapping(value="doRegister",method=RequestMethod.POST)
    public ResponseEntity<Map<String, Object>> registerUser(User user){
        Map<String,Object> map=new HashMap<>();
        try {
            Boolean flag=this.userService.addUser(user);
            if (flag) {
                map.put("status", 200);
                return ResponseEntity.ok(map);
            }

        } catch (Exception e) {

            e.printStackTrace();
        }


        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }


    //用户登录
    @RequestMapping(value="/doLogin", method=RequestMethod.POST)
    @ResponseBody
    public TaotaoResult userLogin(String username, String password,
                                  HttpServletRequest request, HttpServletResponse response) {
        String token = null;
        try {
            token = this.userService.login(username,password);

        if (StringUtils.isNotBlank(token)){
         CookieUtils.setCookie(request,response,"TT_TOKEN",token);
         return  TaotaoResult.ok();
     }
     return TaotaoResult.build(400,"用户名或者密码错误");
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return TaotaoResult.build(500,"服务器内部异常");
        }

    }

    //校验用户
    @RequestMapping(value = "query/{token}",method = RequestMethod.GET)
    public ResponseEntity<User> getuserInfo(@PathVariable("token") String token){

        User user = null;
        try {
            user = this.userService.getUserInfo(token);
            if (user==null){
                return ResponseEntity.status(400).body(null);
            }
            return ResponseEntity.status(HttpStatus.OK).body(user);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }

    }

}
